package com.zzqstart.ossservice.controller;

import com.zzqstart.commonutils.R;
import com.zzqstart.ossservice.service.OssService;
import org.joda.time.DateTime;
import org.springframework.http.codec.multipart.FilePart;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * ClassName: OssController
 * Package: com.atguigu.oss.controller
 * Description:
 *
 * @Author zzqstart
 * @Create 2023-02-22 20:39
 * @Version 1.0
 */
@RestController
@RequestMapping("/ossservice/fileoss")
public class OssController {

    @Resource
    private OssService ossService;

    @PostMapping("/img")
    public R uploadOssImg(MultipartFile file){

        String time = new DateTime().toString("yyyy/MM/dd");
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-","");
        String newFileName = uuid + filename;
        String filePath = time+"/"+newFileName;

        String url = ossService.uploadOssImg(file,filePath);
        return R.ok().data("url",url);
    }


    @PostMapping("/avatar")
    public R uploadMemberAvatar(MultipartFile file){

        String time = new DateTime().toString("yyyy/MM/dd");
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString().replace("-","");
        String newFileName = uuid + filename;
        String filePath = "avatar/"+time+"/"+newFileName;

        String url = ossService.uploadOssImg(file,filePath);
        return R.ok().data("url",url);
    }

    @DeleteMapping("/delOssImg/{imgUrl}")
    public R delOssImg(@PathVariable String imgUrl){
        Boolean isDel = ossService.delOssImg(imgUrl);
        if (isDel){
            return R.ok();
        }else {
            return R.error().message("删除失败");
        }
    }
}
